package cn.sh.ideal.productservice.service.impl;

import cn.sh.ideal.productservice.domain.Product;
import cn.sh.ideal.productservice.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: mr_zhao
 * @Date: 2020/7/4-14:00
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Value("${server.port}")
    private String port;

    private static final Map<Integer, Product> PRODUCT_MAP = new HashMap<>();

    static {
        PRODUCT_MAP.put(1, new Product(1, "小米手机1", 201, 501));
        PRODUCT_MAP.put(2, new Product(2, "小米手机2", 202, 502));
        PRODUCT_MAP.put(3, new Product(3, "小米手机3", 203, 503));
        PRODUCT_MAP.put(4, new Product(4, "小米手机4", 204, 504));
        PRODUCT_MAP.put(5, new Product(5, "小米手机5", 205, 505));
        PRODUCT_MAP.put(6, new Product(6, "小米手机6", 206, 506));
        PRODUCT_MAP.put(7, new Product(7, "小米手机7", 207, 507));
        PRODUCT_MAP.put(8, new Product(8, "小米手机8", 208, 508));
    }


    @Override
    public List<Product> listProduct() {
        List<Product> Product = new ArrayList<>(PRODUCT_MAP.values());
        System.out.println(Product.toString());
        return Product;
    }

    @Override
    public Product findProductById(int id) {
        Product product = PRODUCT_MAP.get(id);

        Product result = new Product();

        BeanUtils.copyProperties(product, result);
        result.setName(result.getName() + "来自服务端口:" + port);
        return result;
    }
}
